#include <iostream>
#include <string>
#include <map>
#include <regex>

class DigParser {
public:
    explicit DigParser(const std::string &dig_output) {
        parse_dig_output(dig_output);
    }

    std::string get_section(const std::string &section_name) {
        auto it = sections.find(section_name);
        if (it != sections.end()) {
            return it->second;
        }
        return "";
    }

private:
    void parse_dig_output(const std::string &dig_output) {
//        std::regex section_regex(R"delim(;; ([\w ]+ SECTION):(\r\n.*?)(?=\r\n\r\n|;;))delim");
        std::regex section_regex(R"delim(;; ([\w ]+ SECTION):(\r?\n.*?)(?=\r?\n\r?\n|;;))delim");
        std::smatch matches;

        std::string::const_iterator search_start(dig_output.begin());
        while (std::regex_search(search_start, dig_output.cend(), matches, section_regex)) {
            sections[matches[1].str()] = matches[2].str();
            search_start = matches.suffix().first;
        }
    }

    std::map<std::string, std::string> sections;
};

int main() {
    std::string dig_output = ";; ANSWER SECTION:\nsample.com.  3600  IN  A 192.168.0.1\n\n;; AUTHORITY SECTION:\nsample.com.  86400  IN  NS ns1.sample.com.\n\n;; ADDITIONAL SECTION:\nns1.sample.com. 86400 IN  A 192.168.1.1\n";

    DigParser dig_parser(dig_output);

    std::cout << "Answer Section:\n" << dig_parser.get_section("ANSWER SECTION") << std::endl;
    std::cout << "Authority Section:\n" << dig_parser.get_section("AUTHORITY SECTION") << std::endl;
    std::cout << "Additional Section:\n" << dig_parser.get_section("ADDITIONAL SECTION") << std::endl;

    return 0;
}
